package com.itheima.web.manager;


import cn.hutool.core.util.StrUtil;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.db.UserStatus;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.db.UserManagementService;
import com.itheima.util.ConstantUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

@Component
public class UserManagementManager {

    @DubboReference
    private UserManagementService userManagementService;

    @DubboReference
    private UserInfoService userInfoService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //用户冻结
    public ResponseEntity userStatus(UserStatus userStatus) throws ParseException {

        UserStatus user = userManagementService.findById(userStatus.getUserId());

        //定时任务 自动解冻
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_YEAR,userStatus.getFreezingTime());
        Date calendarTime = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        Date date = sdf.parse(String.valueOf(calendarTime));
        //创建一个定时器Timer对象
        Timer timer = new Timer();
        //调用Timer对象中的schedule定时方法
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                //设置个人信息为解冻状态
                UserInfo userInfo = new UserInfo();
                userInfo.setId(Long.valueOf(userStatus.getUserId()));
                userInfo.setUserStatus("1");
                userInfoService.update(userInfo);
                //执行完定时任务,终止定时器
                timer.cancel();
            }
        },date);

        if (user == null) {
            //1.先把冻结数据存储到数据库
            userManagementService.saveUserStatus(userStatus);
        }else
        {
            //如果有数据 就更新
            userManagementService.update(userStatus);
        }

        UserInfo userInfo = new UserInfo();
        userInfo.setId(userStatus.getUserId());
        userInfo.setUserStatus("2");
        userInfoService.update(userInfo);
        //2. 存入rdies 数据
        Integer freezingTime = userStatus.getFreezingTime();


        if (freezingTime == 1) {//StrUtil.format(ConstantUtil.MOVEMENT_LIKE, userId, publishId)
            stringRedisTemplate.opsForValue().set(StrUtil.format(ConstantUtil.USERSTATUS_DAYS+userStatus.getUserId()), "1", Duration.ofDays(3));
            System.err.println(freezingTime);
        } else if (freezingTime == 2) {
            stringRedisTemplate.opsForValue().set(StrUtil.format(ConstantUtil.USERSTATUS_DAYS+userStatus.getUserId()), "2", Duration.ofDays(7));
        } else if (freezingTime == 3) {
            stringRedisTemplate.opsForValue().set(StrUtil.format(ConstantUtil.USERSTATUS_DAYS+userStatus.getUserId()), "3", Duration.ofDays(99999));
        }


        String message = "解冻成功";
        return ResponseEntity.ok(message);

    }

    //解冻
    public ResponseEntity unUserStatus(Integer userId, String reasonsForThawing) {

        //设置个人信息为解冻状态
        UserInfo userInfo = new UserInfo();
        userInfo.setId(Long.valueOf(userId));
        userInfo.setUserStatus("1");
        userInfoService.update(userInfo);
        //2.把redis的key 删掉
        stringRedisTemplate.delete(StrUtil.format(ConstantUtil.USERSTATUS_DAYS,userId));
        //3.加入解冻备注
        UserStatus userStatus = new UserStatus();
        userStatus.setFreezingTime(0);
        userStatus.setFreezingRange(0);
        userStatus.setUserId(Long.valueOf(userId));
        userStatus.setReasonsForThawing(reasonsForThawing);
        userManagementService.update(userStatus);

        String message = "解冻成功";
        return ResponseEntity.ok(message);
    }
}
